/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the gradecalculator.net nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.gradecalculator.plugin.core.factory.impl;

import java.util.HashMap;
import java.util.Map;

import net.gradecalculator.core.enums.PassResult;
import net.gradecalculator.core.interfaces.ICourse;
import net.gradecalculator.core.interfaces.results.IResult;
import net.gradecalculator.core.interfaces.results.IResultType;

public class CoreResult implements IResult {

	private int examNumber = 0;
	private Map<IResultType, String> grades = new HashMap<IResultType, String>();
	private PassResult practicalResult;
	private boolean gradePassed;
	private int semester;
	private ICourse course;

	@Override
	public void setCourse(final ICourse course) {
		this.course = course;
	}

	@Override
	public ICourse getCourse() {
		return course;
	}

	@Override
	public boolean isPassed() {
		if (!course.getGradedPerformance().isEmpty()) {
			if (!gradePassed) {
				return false;
			}
		} else {
			if (course.getUngradedPerformance().isEmpty()) {
				return false;
			}
		}

		if (!course.getUngradedPerformance().isEmpty()) {
			if (practicalResult != PassResult.PASSED) {
				return false;
			}
		}

		return true;
	}

	@Override
	public PassResult getUngradedPassed() {
		return practicalResult;
	}

	@Override
	public void setUngradedPassed(final PassResult practicalResult) {
		this.practicalResult = practicalResult;
	}

	@Override
	public Map<IResultType, String> getGrades() {
		return grades;
	}

	@Override
	public void setGrades(final Map<IResultType, String> grades) {
		this.grades = grades;

		if (grades.size() == 1) {
			for (final IResultType type : grades.keySet()) {
				if (type.isPassed()) {
					gradePassed = true;
					return;
				}
			}
		}
		gradePassed = false;
	}

	@Override
	public String getGrade(final IResultType type) {
		if (gradePassed) {
			for (final IResultType passedType : grades.keySet()) {
				return grades.get(passedType);
			}
		}

		if (grades.containsKey(type)) {
			return grades.get(type);
		}

		return "";
	}

	@Override
	public boolean getPracticalResult(final IResultType type) {
		if (!type.isPassed()) {
			return practicalResult != PassResult.NOTPASSED;
		}

		return practicalResult == PassResult.PASSED;
	}

	@Override
	public void setPracticalResult(final PassResult result) {
		practicalResult = result;
	}

	@Override
	public int getExamNumber() {
		return examNumber;
	}

	@Override
	public void setExamNumber(final int examNumber) {
		this.examNumber = examNumber;
	}

	@Override
	public int getSemester() {
		return semester;
	}

	@Override
	public void setSemester(final int semester) {
		this.semester = semester;
	}
}
